//
//  library.m
//  kankanVideo
//
//  Created by wangbo on 7/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "library.h"
#import "XMLParser.h"

@implementation library
@synthesize myScrollView,delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

-(IBAction)back:(id)sender{
    //[delegate moveView:-1];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDidStopSelector:@selector(animationComplete)];
    //sv.contentOffset=CGPointMake(320*nowPage,0);
    [self.view setFrame:CGRectMake(1024, 0, 500, 748)];
    [UIView commitAnimations];
    //[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //MyCoreData *coreData=[[MyCoreData alloc]initWithTableName:@"CategoryData"];
    /*self.view.layer.shadowRadius=5;
    self.view.layer.shadowColor=[[UIColor blackColor] CGColor];
    self.view.layer.shadowOffset=CGSizeMake(5, 3);
    self.view.layer.shadowOpacity=0.8;*/
    listView=[[ChannelList alloc]initWithStyle:UITableViewStylePlain];
    listView.mydelegate=self;
    [listView.view setFrame:CGRectMake(110, 54, 390, 694)];
    [self.view addSubview:listView.view];
    //AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    //[app showAlert:@"正在加载"];
    [[MyRequest shareInstance] loaddataWithURL:@"http://interface.kankanews.com/kkapi/mobile/padapi.php?m=clistphone&c=ios" forObject:self];
    //[[MyRequest shareInstance] loaddataWithURL:@"http://interface.kankanews.com/kkapi/mobile/mobileapi.php?m=clistphone" forObject:self];
}

-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    if(request.responseStatusCode==200){
        //AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
        //[app hideAlert];
        arr=[XMLParser parseCategoryList:request.responseData];
        //NSLog(@"%@",arr);
        NSMutableArray *tmp=[[NSMutableArray alloc]init];
        BOOL fg=NO;
        for (int i=0; i<arr.count; i++) {
            NSMutableArray *arr1=[arr objectAtIndex:i];
            fg=NO;
            for (int j=0; j<tmp.count; j++) {
                //NSMutableArray *tmp2=[tmp objectAtIndex:j];
                if([[tmp objectAtIndex:j]isEqualToString:[arr1 objectAtIndex:6]]){
                    fg=YES;
                }
            }
            if (!fg) {
                [tmp addObject:[arr1 objectAtIndex:6]];
            }
        }
        for (int i=0; i<tmp.count; i++) {
            CategoryItem *item=[[CategoryItem alloc]initWithFrame:CGRectMake(0, (i+1)*61, 110, 60) withTitle:[tmp objectAtIndex:i] withURL:@"" withIndex:[NSNumber numberWithInt:i]];
            item.delegate=self;
            //[item btnTapped:nil];
            [myScrollView addSubview:item];
        }
        [myScrollView setContentSize:CGSizeMake(110, (tmp.count+1)*61)];
        //我的关注 大栏目
        CategoryItem *citem=[[CategoryItem alloc]initWithFrame:CGRectMake(0, 0, 110, 60) withTitle:@"我的订阅" withURL:@"myfocus" withIndex:[NSNumber numberWithInt:-1]];
        citem.delegate=self;
        //[citem myselected];
        [myScrollView addSubview:citem];
        [citem btnTapped:nil];
    }
}

-(void)itemSelected:(NSMutableArray*)item forIndex:(NSNumber *)theIndex{
    //NSLog(@"itemSelected:%@",item);
    
    for (CategoryItem *citem in [myScrollView subviews]) {
        [citem myunselected];
    }
    if (theIndex==[NSNumber numberWithInt:-1]) {
        [listView binddata:item withFlag:NO];
    }else{
        NSLog(@"itemSelected");
        NSMutableArray *data=[[NSMutableArray alloc]init];
        for (int i=0; i<arr.count; i++) {
            NSMutableArray *tt=[arr objectAtIndex:i];
            if ([[tt objectAtIndex:6]isEqualToString:[item objectAtIndex:0]]) {
                //tt objectAtIndex:0
                NSMutableArray *ar=[NSMutableArray arrayWithCapacity:5];
                [ar addObject:[tt objectAtIndex:5]]; //classidid
                [ar addObject:[tt objectAtIndex:0]]; //title
                [ar addObject:[tt objectAtIndex:3]]; //smallpic
                [ar addObject:[tt objectAtIndex:2]]; //bigpic
                [ar addObject:[tt objectAtIndex:7]]; //isgood
                [ar addObject:[tt objectAtIndex:8]]; //firsttitle
                [data addObject:ar];
            }
        }
        NSLog(@"%@",data);
        [listView binddata:data withFlag:YES];
        //[listView binddata:item withFlag:YES];
    }
}

-(void)modifyItem:(int)actionType withParam:(NSMutableArray *)param{
    [delegate modifyListItem:actionType withParam:param];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end
